//index.js
//获取应用实例
const app = getApp();

var route = require('../../utils/route');
var config = require('../../config/config');
var httpClient = require('../../utils/httpClient');

// pages/topicchoose/topicchoose.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchLoading: true,  //把"上拉加载"的变量设为true，显示  
    searchLoadingComplete: false, //把“没有数据”设为false，隐藏 
    currentTab: '0',
    sent: null,
    size: 10,
    miyuCurrent: 1,
    miyuTotalPage: 1,
    miyulist: null,
    chenyuCurrent: 1,
    chenyutalPage: 1,
    chenyulist: null,
    jizhuanwanCurrent: 1,
    jizhuanwantalPage: 1,
    jizhuanwanlist: null,
    qitaCurrent: 1,
    qitatalPage: 1,
    searchScrollLower: 0,
    qitalist: null,
    array: [{
      topic: '测试',
    }, {        
      topic: '测试'
    }],
    wxSearchData: "",
    tabType: 1//tab页默认为1
  },
  //滑动切换
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current,
      tabType: e.detail.current + 1
    });
    this.getHbQuestions(e.detail.current);
  },
  // 点击标题切换当前页时改变样式
  clickTab: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

    var that = this;
    app.getUserInfo(function (res) {
      that.setData({
        userInfo: res
      });
    });

    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 0;
        that.setData({
          winHeight: calc,
          searchScrollLower: calc / 2
        });
      }
    });
    this.getHbQuestions(this.data.currentTab);
  },




  // 搜索框输入时候操作
  wxSearchInput: function (e) {
    var that = this;
    var inputValue = e.detail.value;
    if (inputValue == '') {
      that.setData({
        wxSearchData: ""
      });
      this.getHbQuestions(that.data.tabType - 1);
    } else {
      // 更新视图
      that.setData({
        wxSearchData: inputValue
      });
    }
  },

  // 清空输入
  wxSearchClear: function () {
    var that = this;
    // 更新视图
    that.setData({
      wxSearchData: "",
    });
    this.getHbQuestions(that.data.tabType - 1);
  },// 确任或者回车
  wxSearchConfirm: function (e) {
    var that = this;
    var key = e.target.dataset.key;
    if (key == 'back') {
      wx.navigateBack(1);
    } else {
      that.search(that.data.wxSearchData);
    }
  },


  search: function (inputValue) {
    var that = this;
    if (inputValue && inputValue.length > 0) {
      console.log("ssssssss");
      if (inputValue && inputValue.length > 0) {
        console.log("确保搜索条件无误，开始进行搜索操作" + inputValue);
        this.getHbQuestionsByquestionsName(inputValue);
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    debugger;
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getHbQuestionsNext(this.data.currentTab);

  },


  searchScrollLower: function () {

  },

 

  //根据搜索条件查询题库
  getHbQuestionsByquestionsName: function (questionsName) {  
    debugger
    var that = this;
    var winHeight = that.data.winHeight;
    that.setData({
      searchScrollLower: winHeight / 2
    });

    var method = route.getHbQuestions();
    var params = '';
    if (that.data.tabType == 1) {
      params = {
        questionsName: questionsName,
        titleCassification: 1,
        size: that.data.size
      };
    } else if (that.data.tabType == 2) {
      params = {
        questionsName: questionsName,
        titleCassification: 2, 
        size: that.data.size
      };
    } else if (that.data.tabType == 3) {
      params = {
        questionsName: questionsName,
        titleCassification: 3,
        size: that.data.size
      };
    } else if (that.data.tabType == 4) {
      params = {
        questionsName: questionsName,
        titleCassification: 4,
        size: that.data.size
      };
    }

    httpClient.request(method, 'GET', params, function (res, err) {
      if (that.data.tabType == 1) { 
        that.setData({
          miyulist: res.rows,
          sent: res,
          miyuCurrent: res.current + 1,
          miyuTotalPage: res.pages
        });
      } else if (that.data.tabType == 2) { 
        that.setData({
          chenyulist: res.rows,
          sent: res,
          chenyuCurrent: res.current + 1,
          chenyutalPage: res.pages
        });
      } else if (that.data.tabType == 3) { 
        that.setData({
          jizhuanwanlist: res.rows,
          sent: res,
          jizhuanwanCurrent: res.current + 1,
          jizhuanwantalPage: res.pages
        });
      } else if (that.data.tabType == 4) { 
        that.setData({
          qitalist: res.rows,
          sent: res,
          qitaCurrent: res.current + 1,
          qitatalPage: res.pages
        });
      }

    });
  },












































  getHbQuestions: function (type) {
    debugger
    var that = this;
    var winHeight = that.data.winHeight; 
    that.setData({
      searchScrollLower: winHeight / 2
    });
    var method = route.getHbQuestions();
    var params = '';
    if (type == 0) {
      params = {
        titleCassification: 1,
        size: that.data.size
      };
    } else if (type == 1) {
      params = {
        titleCassification: 2,
        size: that.data.size
      };
    } else if (type == 2) {
      params = {
        titleCassification: 3,
        size: that.data.size
      };
    } else if (type == 3) {
      params = {
        titleCassification: 4,
        size: that.data.size
  
      };
    } else if (that.data.tabType == 3) {
      params = { 
        titleCassification: 3,
        size: that.data.size
      };
    } else if (that.data.tabType == 4) {
      params = { 
        titleCassification: 4,
        size: that.data.size
      };
    }
debugger
    var wxSearchData = that.data.wxSearchData;
    if (wxSearchData && wxSearchData.length > 0) {
      params.questionsName = wxSearchData;
    }

    httpClient.request(method, 'GET', params, function (res, err) {
      if (that.data.tabType == 1) {
        that.setData({
          miyulist: res.rows,
          sent: res,
          miyuCurrent: res.current + 1,
          miyuTotalPage: res.pages
        });
      } else if (that.data.tabType == 2) {
        that.setData({
          chenyulist: res.rows,
          sent: res,
          chenyuCurrent: res.current + 1,
          chenyutalPage: res.pages
        });
      } else if (that.data.tabType == 3) {
        that.setData({
          jizhuanwanlist: res.rows,
          sent: res,
          jizhuanwanCurrent: res.current + 1,
          jizhuanwantalPage: res.pages
        });
      } else if (that.data.tabType == 4) {
        that.setData({
          qitalist: res.rows,
          sent: res,
          qitaCurrent: res.current + 1,
          qitatalPage: res.pages
        });
      }

    });
  },



  getHbQuestionsNext: function (type) {

    var that = this;
    var method = route.getHbQuestions();
    if (type == 0) {
      if (this.data.miyuCurrent > this.data.miyuTotalPage) {
        return;
      }
      var params = {
        titleCassification: type + 1,
        current: that.data.miyuCurrent,
        size: that.data.size
      };
    } else if (type == 1) {
      if (this.data.chenyuCurrent > this.data.chenyutalPage) {
        return;
      }
      var params = {
        titleCassification: type + 1,
        current: that.data.chenyuCurrent,
        size: that.data.size
      };
    } else if (type == 2) {
      if (this.data.jizhuanwanCurrent > this.data.jizhuanwantalPage) {
        return;
      }
      var params = {
        titleCassification: type + 1,
        current: that.data.jizhuanwanCurrent,
        size: that.data.size
      };
    } else if (type == 3) {
      if (this.data.qitaCurrent > this.data.qitatalPage) {
        return;
      }
      var params = {
        titleCassification: type + 1,
        current: that.data.qitaCurrent,
        size: that.data.size
      };
    }
    var wxSearchData = that.data.wxSearchData;
    if (wxSearchData){
      params.questionsName = wxSearchData;
    }

    httpClient.request(method, 'GET', params, function (res, err) {
      debugger;
      if (type == 0) {
        var data = that.data.miyulist;
        if (null != data) {
          var resList = data.concat(res.rows);
          var searchScrollLower = resList.length * 45;
          that.setData({
            miyulist: resList,
            searchScrollLower: searchScrollLower
          });
        } else {
          that.setData({
            miyulist: res.rows,
            sent: res
          });
        }
        if (that.data.miyuCurrent <= res.pages) {
          that.setData({
            miyuCurrent: res.current + 1,
            miyuTotalPage: res.pages
          });
        }
      } else if (type == 1) {
        var data = that.data.chenyulist;
        if (null != data) {
          var resList = data.concat(res.rows);
          var searchScrollLower = resList.length * 45;
          that.setData({
            chenyulist: resList,
            searchScrollLower: searchScrollLower
          });
        } else {
          that.setData({
            chenyulist: res.rows,
            sent: res
          });
        }
        if (that.data.chenyuCurrent <= res.pages) {
          that.setData({
            chenyuCurrent: res.current + 1,
            chenyutalPage: res.pages
          });
        }
      } else if (type == 2) {
        var data = that.data.jizhuanwanlist;
        if (null != data) {
          var resList = data.concat(res.rows);
          var searchScrollLower = resList.length * 45;
          that.setData({
            jizhuanwanlist: resList,
            searchScrollLower: searchScrollLower
          });
        } else {
          that.setData({
            jizhuanwanlist: res.rows,
            sent: res
          });
        }
        if (that.data.jizhuanwanCurrent <= res.pages) {
          that.setData({
            jizhuanwanCurrent: res.current + 1,
            jizhuanwantalPage: res.pages
          });
        }
      } else if (type == 3) {
        var data = that.data.qitalist;
        if (null != data) {
          var resList = data.concat(res.rows);
          var searchScrollLower = resList.length * 45;
          that.setData({
            qitalist: resList,
            searchScrollLower: searchScrollLower
          });
        } else {
          that.setData({
            qitalist: res.rows,
            sent: res
          });
        }
        if (that.data.qitaCurrent <= res.pages) {
          that.setData({
            qitaCurrent: res.current + 1,
            qitatalPage: res.pages
          });
        }
      }

    });
  },
  questionsInfo: function (e) {

    var quesNo = e.currentTarget.dataset.redpacketnum;
    var quesName = e.currentTarget.dataset.obj;
    var titleCassification = e.currentTarget.dataset.type;
    app.globalData.quesNo = quesNo;
    app.globalData.quesName = quesName;
    app.globalData.titleCassification = titleCassification;
    
    wx.navigateBack({})
    /*
        wx.switchTab({
          url: '../index/index'
        })
        */
  },





})